﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using TCKQKL.Service.DataContract;
using TCKQKL.Service.Config;
using TCKQKL.Service.Contract;
using TCKQKL.DAL.IDAL;
using System.Net.Security;
using StockTrader.BusinessService.DataContract.Mortgate;
using Mortgage.DDL;

namespace Mortgage.DAL
{
    public class LookupServiceDAL
    {
        private IInvestor dalInvestor;
        private IOrdersBosc dalOrder;
        private IMarketSummary dalMarket;
        private IOrdersOnline dalOrdersOnline;

        public void PrintMessage(string message)
        {
            PrintSecurityInformation();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Message received [{0}]", message);
            Console.ResetColor();
        }

        private void PrintSecurityInformation()
        {
        }

        public DateTime getTradingDate()
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getTradingDate();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public InvestorDataModel getAccountData(string accountNo)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getInvestorData(accountNo);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<InvestorDataModel> getAssignInvestorData(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getAssignInvestorData(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<InvestorDataModel> getAssignInvestorDataByName(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getAssignInvestorDataByName(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public InvestorDataProfileModel getAccountProfileData(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getInvestorProfileData(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public InvestorDataModel loginProcess(string accountNo, string password)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.login(accountNo, password);
            }
            catch
            {
                return null;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public void logout(string accountNo)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            try
            {
                dalInvestor.logOutUser(accountNo);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<TransDataModel> getTransByInvestorId(decimal investorID, DateTime fromDate, DateTime toDate)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getTransByInvestorId(investorID, fromDate, toDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public decimal getRemain(decimal investorID, DateTime selected)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getRemain(investorID, selected);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrders(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrders(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersWaiting(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersWaiting(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersTopN(decimal investorID, int topN)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersTopN(investorID, topN);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public List<OrdersBoscModel> getOrdersHistory(decimal investorID, DateTime start, DateTime end)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersHistory(investorID, start, end);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersHistoryDetail(decimal investorID, DateTime fromDate, DateTime toDate, string orderType, string orderClass, string orderSide, string symbol, string ID, decimal priceLow, decimal priceHigh, decimal qttyLow, decimal qttyHigh)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersHistoryDetail(investorID, fromDate, toDate, orderType, orderClass, orderSide, symbol, ID, priceLow, priceHigh, qttyLow, qttyHigh);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<AccountDataModel> getAccountByInvestorID(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getAccountDataByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<BankDataModel> getBankByInvestorID(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getBankDataByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public BankDataModel getBankDataByDate(decimal investorID, DateTime selected)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getBankDataByDate(investorID, selected);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public decimal getNettingAmountByInvestorId(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getNettingAmountByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResult(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResult(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultHistory(decimal investorID, DateTime start, DateTime end)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResultHistory(investorID, start, end);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersRelatedByOrderId(string originOrderId)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getOrdersRelatedByOrderId(originOrderId);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersRelatedHistoryByOrderId(string originOrderId)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getOrdersRelatedHistoryByOrderId(originOrderId);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultByOrderID(string orderID)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getMatchedResultByOrderId(orderID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultTopN(decimal investorID, int topN)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResultTopN(investorID, topN);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public List<MatchedResultModel> getMatchedResultEarlyByAccountNo(string orderID)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getMatchedResultEarlyByAccountNo(orderID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultByInvestorId(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResultByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public MarketSummaryDataModel getMarketSummary(string marketName)
        {
            dalMarket = TCKQKL.DAL.DataFactory.MarketSummary.Create(Settings.SQLDAL);
            dalMarket.Open(Settings.CONFIGDB_SQL_CONN_STRING);
            try
            {
                if (marketName.Equals("HOSE"))
                    return dalMarket.getMarketSummaryHose();
                else
                    return dalMarket.getMarketSummaryHastc();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalMarket.Close();
            }
        }

        public StocksDataModel getQuote(string symbol)
        {
            dalMarket = TCKQKL.DAL.DataFactory.MarketSummary.Create(Settings.SQLDAL);
            dalMarket.Open(Settings.CONFIGDB_SQL_CONN_STRING);
            try
            {
                if (dalMarket.getQuoteHose(symbol) != null)
                    return dalMarket.getQuoteHose(symbol);
                else if (dalMarket.getQuoteHastc(symbol) != null)
                    return dalMarket.getQuoteHastc(symbol);
                else
                    return dalMarket.getQuoteUpcom(symbol);

            }
            catch
            {
                throw;
            }
            finally
            {
                dalMarket.Close();
            }
        }

        public StocksDataModel getQuoteAvg(string symbol, bool isAvg)
        {
            dalMarket = TCKQKL.DAL.DataFactory.MarketSummary.Create(Settings.SQLDAL);
            dalMarket.Open(Settings.CONFIGDB_SQL_CONN_STRING);
            try
            {
                if (isAvg)
                {
                    if (dalMarket.getQuoteHoseAvg(symbol) != null)
                        return dalMarket.getQuoteHoseAvg(symbol);
                    else if (dalMarket.getQuoteHastcAvg(symbol) != null)
                        return dalMarket.getQuoteHastcAvg(symbol);
                    else
                        return dalMarket.getQuoteUpcomAvg(symbol);
                }
                else
                {
                    if (dalMarket.getQuoteHose(symbol) != null)
                        return dalMarket.getQuoteHose(symbol);
                    else if (dalMarket.getQuoteHastc(symbol) != null)
                        return dalMarket.getQuoteHastc(symbol);
                    else
                        return dalMarket.getQuoteUpcom(symbol);
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                dalMarket.Close();
            }
        }

        public List<DateTime> getBackDate()
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getBackDate();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public decimal getPreLoanByInvestor(decimal investorid)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getPreLoanByInvestor(investorid);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public List<AccountDataModel> getStockNettingInvestorId(decimal investorID, DateTime transdate)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getStockNettingInvestorId(investorID, transdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public bool isClearingDone()
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.isClearingDone();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public AccountData getCustomerInfo(string AccountNo)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getCustomerInfo(AccountNo);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }


    }
}
